package com.sk.k01.m01.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@ApiModel(value = "全局统一返回结果")
@Data
public class Msg {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    /**
     * 构造方法私有化，不能new
     */
    private Msg(){}

    public static Msg success(){
        Msg msg = new Msg();
        msg.setSuccess(true);
        msg.setCode(ResultCode.SUCCESS);
        msg.setMessage("成功！");
        return msg;
    }

    public static Msg error(){
        Msg msg = new Msg();
        msg.setSuccess(false);
        msg.setCode(ResultCode.ERROR);
        msg.setMessage("失败！");
        return msg;
    }

    public Msg success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public Msg message(String message){
        this.setMessage(message);
        return this;
    }

    public Msg code(Integer code){
        this.setCode(code);
        return this;
    }

    public Msg data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public Msg data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
}
